/* 折线图开始 */
var myChar = echarts.init(document.getElementById('main'));
//视图自适应
window.onresize = myChar.resize;
function echart(xdata,data,name) {
    // 指定图表的配置项和数据
    var option = {
        tooltip: {},
        legend: {
            data: [name],
            bottom: 10
        },
        xAxis: {
            data:xdata
        },
        yAxis: {
            type: 'value',
            position: 'left'
        },
        series: [
            {
                name: name,
                type: 'line',
                data: data,
                areaStyle: {}
            }
        ]
    };
    // 使用刚指定的配置项和数据显示图表。
    myChar.setOption(option);
}
//刷新
$("#refresh").click(function () {
    location.reload();
});
//上架商品总数
getGoodsSum(1);
getCourseStatusSum
function getGoodsSum(productSate) {
    $.ajax({
        url: '/welcome/getGoodsSum',
        type: 'post',
        dataType: 'json',
        data: {'productSate': productSate},
        success: function (data) {
            $('.goodsSum').text(data.data);
        }
    });
}

//上架视频总数
getCourseStatusSum(2);

function getCourseStatusSum(courseStatus) {
    $.ajax({
        url: '/welcome/getCourseStatusSum',
        type: 'post',
        dataType: 'json',
        data: {'courseStatus': courseStatus},
        success: function (data) {
            $('.courseSum').text(data.data);
        }
    });
}
//上传视频总数
getCourseSum();
function getCourseSum() {
    $.ajax({
        url: '/welcome/getCourseSum',
        type: 'post',
        dataType: 'json',
        success: function (data) {
            $('#courseSum').text(data.data);
        }
    });
}

//发布微圈总数
getMicrocircleSum();

function getMicrocircleSum() {
    $.ajax({
        url: '/welcome/getMicrocircleSum',
        type: 'post',
        dataType: 'json',
        success: function (data) {
            $('#microcircleSum').text(data.data);
        }
    });
}

//已完成订单总数
getOrderSum(6, $('#orderSum'));
//退订待审核量
getOrderSum(7, $('#unsubscribeCount'));

function getOrderSum(orderStatus, Id) {
    $.ajax({
        url: '/welcome/getOrderSum',
        type: 'post',
        dataType: 'json',
        data: {'orderStatus': orderStatus},
        success: function (data) {
            Id.text(data.data);
        }
    });
}

//商品待审核量
getExamineSum('2', 3, $('#goodsExamineCount'));
//课程待审核量
getExamineSum('2', 2, $('#courseExamineCount'));
//微圈待审核量
getExamineSum('2', 4, $('#microcircleCount'));

//商品、课程、微圈待审核量
function getExamineSum(result, type, Id) {
    $.ajax({
        url: '/welcome/getExamineSum',
        type: 'post',
        dataType: 'json',
        data: {'result': result, 'type': type},
        success: function (data) {
            Id.text(data.data);
        }
    });
}

//今日新增用户
getTodayUserSum();
function getTodayUserSum() {
    $.ajax({
        url: '/welcome/getTodayUserSum',
        type: 'post',
        dataType: 'json',
        success: function (data) {
            $('#todayUserSum').text(data.data);
        }
    });
}

//昨日新增用户
getYesterdayUserSum();
function getYesterdayUserSum() {
    $.ajax({
        url: '/welcome/getYesterdayUserSum',
        type: 'post',
        dataType: 'json',
        success: function (data) {
            $('#yesterdayUserSum').text(data.data);
        }
    });
}

//本月新增用户
getMonthUserSum();
function getMonthUserSum(result, type, Id) {
    $.ajax({
        url: '/welcome/getMonthUserSum',
        type: 'post',
        dataType: 'json',
        success: function (data) {
            $('#monthUserSum').text(data.data);
        }
    });
}

//全部用户用户
getUserSum();
function getUserSum(result, type, Id) {
    $.ajax({
        url: '/welcome/getBeforeUserSum',
        type: 'post',
        dataType: 'json',
        success: function (data) {
            $('#userSum').text(data.data);
        }
    });
}
//本月新增课程总数
getCourseMonthAddCount();
function getCourseMonthAddCount() {
    $.ajax({
        url:'/welcome/getCourseMonthAddCount',
        type:'post',
        dataType:'json',
        success:function (data) {
            $('#addCount').text(data.data);
        }
    });
}

//本月新增课程同比上月百分比
getcoursePercentage();
function getcoursePercentage() {
    $.ajax({
        url:'/welcome/getCourseAddLastMonthCount',
        type:'post',
        dataType:'json',
        success:function (data) {
            $.ajax({
                url:'/welcome/getCourseMonthAddCount',
                type:'post',
                dataType:'json',
                success:function (item) {
                    $('#percentage').text(toPercent((item.data-data.data)/data.data))
                }
            });
        }
    });
}
//本月新增商品
function getGoodsMonthAddSum() {
    $.ajax({
        url: '/welcome/getGoodsMonthAddSum',
        type: 'post',
        dataType: 'json',
        success: function (data) {
            $('#addCount').text(data.data);
        }
    });
}
//本月新增商品同比上月百分比
function goodsPercentage() {
    $.ajax({
        url:'/welcome/getGoodsLastMonthAddSum',
        type:'post',
        dataType:'json',
        success:function (data) {
            $.ajax({
                url:'/welcome/getGoodsMonthAddSum',
                type:'post',
                dataType:'json',
                success:function (item) {
                    $('#percentage').text(toPercent((item.data-data.data)/data.data))
                }
            });
        }
    });
}
//本月新增用户
function getMonthUserSum() {
    $.ajax({
        url: '/welcome/getMonthUserSum',
        type: 'post',
        dataType: 'json',
        success: function (data) {
            $('#addCount').text(data.data);
        }
    });
}
//本月新增用户同比上月百分比
function getUserPercentage() {
    $.ajax({
        url:'/welcome/getUserAddLastMonthCount',
        type:'post',
        dataType:'json',
        success:function (data) {
            $.ajax({
                url: '/welcome/getMonthUserSum',
                type:'post',
                dataType:'json',
                success:function (item) {
                    $('#percentage').text(toPercent((item.data-data.data)/data.data))
                }
            });
        }
    });
}
//课程统计
getWeekCount('course_id','js_course','课程量');
$('#courseWeekCount').on('click',function () {
    getWeekCount('course_id','js_course','课程量');
    getcoursePercentage();
    getCourseMonthAddCount();
    $('#name').text('课程');
});
//商品统计
$('#goodsWeekCount').on('click',function () {
    getWeekCount('product_id','js_product','商品量');
    goodsPercentage();
    getGoodsMonthAddSum();
    $('#name').text('商品');
});
//用户统计
$('#userWeekCount').on('click',function () {
    getWeekCount('user_id','js_before_user','用户量');
    getUserPercentage();
    getMonthUserSum();
    $('#name').text('用户');
});


//本周新增量
function getWeekCount(field,table,name) {
    $.ajax({
        url:'/welcome/getWeekCount',
        type:'post',
        dataType:'json',
        data:{'field':field,'table':table},
        success:function (data) {
            echart(data.data.list,data.data.data,name)
        }
    });
}


//小数转百分比
function toPercent(point){
    var str=Number(point*100).toFixed(0);
    if (str=='NaN'){
        str = 0;
    }
    str+="%";
    return str;
}
$(".quick").on('click',function () {
    parent.$('#Jump').val($(this).attr('data-value'));
    parent.getQuick();
    return;
});